/****************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxvendpartexptapi.p                              */
/* PROCEDURE TYPE        : API export Process                               */
/* DESCRIPTION           : Supplier Part  export Process                    */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                                23-May-24  */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}
{us/px/pxmaint.i}
{us/px/pxphdef.i sosodxr}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical initial yes      no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram         as character                no-undo.
define variable vc_traceid            as character                no-undo.





define temp-table ttSearchCondition no-undo

        field supplier_from         like vd_mstr.vd_addr
        field supplier_to           like vd_mstr.vd_addr
        .


 define temp-table ttvendpart no-undo

        field vp_part              like vp_mstr.vp_part
        field vp_vend              like vp_mstr.vp_vend
        field vp_vend_part         like vp_mstr.vp_vend_part
        field vp_um                like vp_mstr.vp_um
        field vp_comment           like vp_mstr.vp_comment
        field vp_site              like si_site
        field vp_domain            like vp_mstr.vp_domain
        field vp_datafrom          as   character
        
        index idx1 vp_part vp_vend vp_vend_part vp_domain.

                    

  
   DEFINE DATASET dsSearchCondition FOR ttSearchCondition.

   hdataset = dataset dsSearchCondition:HANDLE.   
           
   DEFINE DATASET dsttvendpart FOR ttvendpart.

   hresult = dataset dsttvendpart:HANDLE.    
                
output to /home/mfg/work/cim/suppvend.log.
mainloop:
repeat on error undo mainloop, leave mainloop:
  
 
   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "vd/"
       m_archpath    = m_archpath + "vd/"
       m_successpath = m_successpath + "vd/"
       m_errorpath   = m_errorpath + "vd/"
       m_workpath    = m_workpath + "vd/".


   empty temp-table ttvendpart.
   
   
   /* Load SEARCH conditions from JSON  */ 
   
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
                    
  if vl_RetOK then do:

    for each ttSearchCondition:

       if ttSearchCondition.supplier_to = "" then ttSearchCondition.supplier_to = hi_char.

       for each vp_mstr no-lock where vp_mstr.vp_domain = global_domain
                                  and vp_mstr.vp_vend >= ttSearchCondition.supplier_from
                                  and vp_mstr.vp_vend <= ttSearchCondition.supplier_to.
                                  
        find ttvendpart where ttvendpart.vp_vend      = vp_mstr.vp_vend 
                          and ttvendpart.vp_part      = vp_mstr.vp_part 
                          and ttvendpart.vp_vend_part = vp_mstr.vp_vend_part 
                          and ttvendpart.vp_domain    = global_domain
                          no-error.
          if not available ttvendpart then 
           do:
           create ttvendpart.
           assign           
           
                  ttvendpart.vp_vend       =    vp_mstr.vp_part
                  ttvendpart.vp_part       =    vp_mstr.vp_vend
                  ttvendpart.vp_vend_part  =    vp_mstr.vp_vend_part
                  ttvendpart.vp_um         =    vp_mstr.vp_um
                  ttvendpart.vp_comment    =    vp_mstr.vp_comment
                  ttvendpart.vp_domain     =    vp_mstr.vp_domain
                  ttvendpart.vp_datafrom   =    "vp_mstr"
                  .
           end. /* if not available ttvendpart then  */
        end.    /* for each vp_mstr no-lock where vp_mstr. */
 

   
       for each po_mstr no-lock where po_domain = global_domain
                                  and po_vend >= ttSearchCondition.supplier_from
                                  and po_vend <= ttSearchCondition.supplier_to
                                  ,
                         each pod_det where pod_domain = global_domain
                                        and pod_nbr = po_nbr
                                        and pod_type <> "M" no-lock.  /* mod by tianyu 240706ss */
             
          find ttvendpart where ttvendpart.vp_vend      = po_mstr.po_vend 
                            and ttvendpart.vp_part      = pod_det.pod_part 
                            and ttvendpart.vp_vend_part = pod_det.pod_vpart 
                            and ttvendpart.vp_domain    = global_domain
                            no-error.
                            
          if not available ttvendpart then 
           do:
           create ttvendpart.
           assign           
           
                  ttvendpart.vp_vend       =    po_mstr.po_vend
                  ttvendpart.vp_part       =    pod_det.pod_part
                  ttvendpart.vp_vend_part  =    pod_det.pod_vpart
                  ttvendpart.vp_um         =    pod_det.pod_um
                  ttvendpart.vp_comment    =    pod_det.pod_desc
                  ttvendpart.vp_domain     =    pod_det.pod_domain
                  ttvendpart.vp_datafrom   =    "POD_det"
                  .
                                                   
                                  
          end.      /* if not available ttvendpart then  */                     
        end. /* for each po_mstr no-lock where po_domain = global_domain */
        
        for each ptp_det where ptp_vend >= ttSearchCondition.supplier_from
                           and ptp_vend <= ttSearchCondition.supplier_to
                           and ptp_vend <> ""
                           and ptp_domain = global_domain
                           no-lock.
                           
              find first ttvendpart where ttvendpart.vp_vend  = ptp_det.ptp_vend 
                            and ttvendpart.vp_part      = ptp_det.ptp_part 
                            and ttvendpart.vp_domain    = global_domain
                            no-error.
        find pt_mstr no-lock where pt_domain = global_domain 
                               and pt_part = ptp_part
                               no-error.
                               
          if not available ttvendpart then 
           do:
           create ttvendpart.
           assign           
           
                  ttvendpart.vp_vend       =    ptp_det.ptp_vend
                  ttvendpart.vp_part       =    ptp_det.ptp_part
                  ttvendpart.vp_vend_part  =    ""
                  ttvendpart.vp_um         =    pt_mstr.pt_um
                  ttvendpart.vp_comment    =    ""
                  ttvendpart.vp_domain     =    ptp_det.ptp_domain
                  ttvendpart.vp_datafrom   =    "ptp_det" .
           
           end. /* if not available ttvendpart then  */           
                           
                           
        end. /* for each ptp_det where ptp_vend >= ttSearchCondition.supplier_from */
       
        for each pt_mstr where pt_vend >= ttSearchCondition.supplier_from
                           and pt_vend <= ttSearchCondition.supplier_to
                           and pt_vend <> ""
                           and pt_domain = global_domain
                           no-lock.
                           
              find first ttvendpart where ttvendpart.vp_vend      = pt_mstr.pt_vend 
                                and ttvendpart.vp_part      = pt_mstr.pt_part 
                                and ttvendpart.vp_domain    = global_domain
                                no-error.
      
                               
          if not available ttvendpart then 
           do:
           create ttvendpart.
           assign           
           
                  ttvendpart.vp_vend       =    pt_mstr.pt_vend
                  ttvendpart.vp_part       =    pt_mstr.pt_part
                  ttvendpart.vp_vend_part  =    ""
                  ttvendpart.vp_um         =    pt_mstr.pt_um
                  ttvendpart.vp_comment    =    ""
                  ttvendpart.vp_domain     =    pt_mstr.pt_domain
                  ttvendpart.vp_datafrom   =    "pt_mstr" .
           
           end. /* if not available ttvendpart then  */           
                           
                           
        end. /* for each ptp_det where ptp_vend >= ttSearchCondition.supplier_from */
       
       
     
       
       
       end. /* for each ttSearchCondition: */
   end.  /* if vl_RetOK then do: */ 
                    
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
      
   
   
   leave.
end. /* repeat on error undo mainloop, leave mainloop: */
